/*
 * @Author: sunzhijun 
 * @Date: 2019-08-02 10:20:30 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2020-01-09 13:43:29
 */
import CommonAction from './common';
import renderPhotoItem from '../container/functions/renderPhoto';
import deepCopy from 'src/hrpub/common/utils/deep-copy';

export default class UniquemodalAction extends CommonAction{
    constructor(comp) {
        super();
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    //隐藏弹窗
    hiddenModal = ()=>{
        const { props } = this.comp;
        const { main, form } = props;
        form.EmptyAllFormValue('uniqueTemp');
        this.dispatch({
            type: 'main/update',
            payload: {
                isModalVisible: false
            }
        });
    }

    //新增唯一性信息校验1
    addUniqueInfo = async ()=>{
        const { props } = this.comp;
        const { main, form } = props;
        let isCheckForm = form.isCheckNow('uniqueTemp');
        if(!isCheckForm){
            return;
        }
        let formData = form.getAllFormValue('uniqueTemp');
        let pk_psncl = formData.rows[0].values.hi_psnjob_pk_psncl.value;
        let res = await this.dispatch({
            type: 'main/isCheckUniqueOne',
            payload: {
                model : formData
            }
        });
        if (res.success) {
            if(res.data.confirmMsg) {
                this.promptBox({
                    color: 'warning',
                    title: '',
                    content: (
                        <div 
                            dangerouslySetInnerHTML={{
                                __html: res.data.confirmMsg
                            }}
                        ></div>
                    ),
                    beSureBtnClick: () => {
                        this.isCheckUniqueTwo(formData,pk_psncl);
                    }
                });
            }else{
                this.isCheckUniqueTwo(formData,pk_psncl);
            }
        }
    }
    //新增唯一性信息校验2
    isCheckUniqueTwo = async (models,pk_psncl)=>{
        const { props, action } = this.comp;
        const { main, form, cardTable, meta } = props;
        let res = await this.dispatch({
            type: 'main/isCheckUniqueTwo',
            payload: {
                model : models,
                pk_org: main.pk_org,
                pk_psncl: pk_psncl,
                uniqueFields: main.uniqueFields
            }
        });
        if (res.success) {
            let data = res.data;
            let template = meta.getMeta();
            if(data.confirmMsg) {
                this.promptBox({
                    color: 'warning',
                    title: '',
                    content: (
                        <div 
                            dangerouslySetInnerHTML={{
                                __html: data.confirmMsg
                            }}
                        ></div>
                    ),
                    beSureBtnClick: () => {
                        this.getAddData(data)
                    }
                });
            }else {
                this.getAddData(data)
            }
        }
    }

    getAddData = async(data) => {
        const { props, action } = this.comp;
        const { main, form, cardTable, meta } = props;
        let template = meta.getMeta();
        let {
            areaCodeList,
            allArea,
            // busiSet
        } = data;
        !!areaCodeList.length && areaCodeList.map((item, index)=>{
            template[item] = deepCopy(allArea[item]); //此处做法是为了隔离配置的模板数据，若不如此 createEditTable 会报错误
            // tableList.push(item);
        })
        meta.setMeta(template);
        form.EmptyAllFormValue('uniqueTemp');
        let hi_psnjob_h = data.psnjobForm.hi_psnjob_h,
            hi_psnorg_h = data.psnorgForm.hi_psnorg_h,
            bd_psndoc = data.psndocForm.bd_psndoc,
            certGrid = data.certGrid,
            hi_psndoc_cert = data.certGrid.hi_psndoc_cert;
            areaCodeList = data.areaCodeList;
        await this.dispatch({
            type: 'main/update',
            payload: {
                isShowSub: false,
                conHeight: '100%',
                isModalVisible: false,
                isCardForm: true,
                ui_state: 1,
                areaCodeList: areaCodeList,
                pk_psncl:  data.PK_psncl,                       //人员主键
                hi_psnjob_h: hi_psnjob_h,
                hi_psnorg_h: hi_psnorg_h,
                // bd_psndoc: bd_psndoc,
                hi_psndoc_cert: hi_psndoc_cert,
                InJobType: data.InJobType,                      //默认入职类型
                pk_psnjob: '',
                pk_psndoc: ''
            }
        })
        // if(!main.areaCodeList.length){
        //     await this.dispatch({
        //         type: 'main/update',
        //         payload: {
        //             areaCodeList: areaCodeList,
        //             busiSet: busiSet
        //         }
        //     });
        // }
        renderPhotoItem.call(this.comp);
        action.tableAct.setIsDisableUpload(false);
        form.setAllFormValue({
            'bd_psndoc': bd_psndoc
        });       
        cardTable.toggleCardTable(areaCodeList, false);  
        cardTable.toggleCardTable('hi_psnjob', true);
        action.cardAct.editCard();    
        let cerGridArr = Object.keys(certGrid);
        if( Array.isArray(cerGridArr) ){
            areaCodeList.map((item, index) => {
                cerGridArr.map((v , k) => {
                    if(item === v){
                        cardTable.setTableData(v, certGrid[v] || [] , null, false);
                    }
                })
            })
        }
        // cardTable.setTableData('hi_psnjob', hi_psnjob_h);
    }
    onAfterEvent = (props, moduleId, key, value, oldValue)=>{
    }
}